#include "StdAfx.h"
#include "VItemPropertyBrowser.h"

// VItemPropertyBrowser ///////////////////////////////////////////////////////

//
VItemPropertyBrowser::VItemPropertyBrowser(QObject *parent)
	:QObject(parent)
{
	m_browser = new	QtTreePropertyBrowser();
	m_manager = new QtVariantPropertyManager(this);

	QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory(this);
	m_browser->setFactoryForManager(m_manager,variantFactory);

	connect(
		m_manager,SIGNAL(
			valueChanged(QtProperty *, const QVariant &)
		),
		this,SLOT(
			browserValueChanged(QtProperty *, const QVariant &)
		)
	);
}

//
VItemPropertyBrowser::~VItemPropertyBrowser()
{
	clearAll();
	delete m_browser;
}

//
void VItemPropertyBrowser::clearAll()
{
	QMap<QtProperty*,QString>::ConstIterator itProp = m_propertyToId.constBegin();
	while (itProp != m_propertyToId.constEnd())
	{
		delete itProp.key();
		itProp++;
	}
	m_propertyToId.clear();
	m_idToProperty.clear();
}

//
void VItemPropertyBrowser::setTreeModel(VTreeModel *treeModel)
{
	m_treeModel = treeModel;

	m_treeModel->disconnect(this);
	connect(
		m_treeModel,SIGNAL(
			dataChanged(const QModelIndex &,const QModelIndex &)
		),
		this,SLOT(
			modelDataChanged(const QModelIndex &,const QModelIndex &)
		)
	);

	clearAll();
}

//
void VItemPropertyBrowser::setTreeIndex(const QModelIndex &treeIndex)
{
	m_treeIndex = treeIndex;
	clearAll();
	
	VTreeNode *node = m_treeModel->nodeFromIndex(m_treeIndex);//static_cast<VTreeNode*>(index.internalPointer());
	VProperty itemProp(node->item());

	//for (int column = 0; column < m_treeModel->columnCount(index); ++column)
	for (int column = 0; column < itemProp.count(); ++column)
	{
		//QModelIndex dataIndex = m_treeModel->index(index.row(),column,index.parent());
		QVariant propValue = itemProp.value(column);//m_treeModel->data(dataIndex,Qt::EditRole);
		QString propName = itemProp.name(column);

		if (qVariantCanConvert<VIntegerList>(propValue))
			propValue = "VIntegerList";
			
		QMetaProperty mp = itemProp.metaProperty(column);
		if (mp.isEnumType())
		{
			QtVariantProperty *prop = m_manager->addProperty(QtVariantPropertyManager::enumTypeId(),propName);
			Q_ASSERT(prop);
			prop->setAttribute("enumNames",VPropertyEnum(mp).displayList());
			prop->setValue(propValue);
			addProperty(prop,propName);
		}
		else
		{
			QtVariantProperty *prop = m_manager->addProperty(propValue.type(),propName);
			Q_ASSERT(prop);
			prop->setValue(propValue);
			addProperty(prop,propName);
		}
	}
}

//
void VItemPropertyBrowser::addProperty(QtVariantProperty *prop, const QString &id)
{
	m_propertyToId[prop] = id;
	m_idToProperty[id] = prop;
	
	QtBrowserItem *item = m_browser->QtTreePropertyBrowser::addProperty(prop);
	if (m_idToExpanded.contains(id))
		m_browser->QtTreePropertyBrowser::setExpanded(item, m_idToExpanded[id]);
}

//
void VItemPropertyBrowser::browserValueChanged(QtProperty *prop,const QVariant &value)
{
	if (!m_propertyToId.contains(prop))
		return;

	QString name = m_propertyToId[prop];
	VTreeNode *node = m_treeModel->nodeFromIndex(m_treeIndex);//static_cast<VTreeNode*>(m_treeIndex.internalPointer());
	VProperty itemProp(node->item());
	int column = itemProp.propIndex(name);
	QModelIndex dataIndex = m_treeModel->index(m_treeIndex.row(),column,m_treeIndex.parent());
	m_treeModel->setData(dataIndex,value,Qt::EditRole);
}

//
void VItemPropertyBrowser::modelDataChanged
(
	const QModelIndex &topLeft,
	const QModelIndex &bottomRight
)
{
	QModelIndex index = topLeft;
	if (m_treeIndex.internalPointer() != index.internalPointer())
		return;
		
	VTreeNode *node = m_treeModel->nodeFromIndex(index);//static_cast<VTreeNode*>(index.internalPointer());
	VProperty itemProp(node->item());
	QString name = itemProp.name(index.column());
	QtVariantProperty *prop = m_idToProperty[name];

	QVariant value = index.data(Qt::EditRole);
	prop->setValue(value);
}
